const fs = require('fs');

function copyDirSync(src, dist, callback, isRoot) {
  try{
    fs.accessSync(dist)
  }catch(err){
    fs.mkdirSync(dist);
  }
  _copy(null, src, dist);
  function _copy(err, src, dist) {
    if(err){
      callback(err);
    } else {
      const paths = fs.readdirSync(src)
      if(paths){
        paths.forEach(function(path) {
          const _src = src + '/' +path;
          const _dist = dist + '/' +path;
          const stat = fs.statSync(_src)
          if(stat.isFile()) {
            fs.writeFileSync(_dist, fs.readFileSync(_src));
          } else if(stat.isDirectory()) {
            // 当为目录时，递归复制
            copyDirSync(_src, _dist, callback, true)
          }
        })
        if(!isRoot)callback() 
      }
    }
  }
}

module.exports ={
  copyDirSync
}